﻿
Public Class signednode(Of T)
#If RETIRED Then
    Inherits IComparable2(Of signednode(Of T))
#End If
    Implements ICloneable, IComparable(Of signednode(Of T)), IComparable

    Private _signingIndex As Int32
    Private sign As UInt32
    Private d As T

    Public Function signingIndex() As Int32
        Return _signingIndex
    End Function

    Public Property data() As T
        Get
            Dim rtn As T = Nothing
            copy(rtn, d)
            Return rtn
        End Get
        Set(ByVal value As T)
            copy(d, value)
            sign = signing(d, signingIndex())
        End Set
    End Property

    Public Property key() As T
        Get
            Return data()
        End Get
        Set(ByVal value As T)
            data() = value
        End Set
    End Property

    Public Shared Widening Operator CType(ByVal that As signednode(Of T)) As T
        Return +that
    End Operator

    Public Shared Operator +(ByVal this As signednode(Of T)) As T
        If this Is Nothing Then
            Return Nothing
        Else
            Return this.data()
        End If
    End Operator

    Public Function CompareTo(ByVal that As signednode(Of T)) As Integer _
                                                                Implements IComparable(Of signednode(Of T)).CompareTo
        assert(signingIndex() = that.signingIndex(), "signingIndex is not equal, cannot compare.")
        Dim cmp As Int32
        cmp = compare(sign, that.sign)
        If cmp = 0 Then
            cmp = compare(d, that.d)
        End If

        Return cmp
    End Function

    Public Function CompareTo(ByVal that As Object) As Integer Implements IComparable.CompareTo
        Return CompareTo(convertor.cast(Of signednode(Of T))(that))
    End Function

    Public Shared Operator =(ByVal this As signednode(Of T), ByVal that As Object) As Boolean
        Return operatorEqual(this, that)
    End Operator

    Public Shared Operator <>(ByVal this As signednode(Of T), ByVal that As Object) As Boolean
        Return operatorUnequal(this, that)
    End Operator

    Public Shared Operator =(ByVal this As signednode(Of T), ByVal that As signednode(Of T)) As Boolean
        Return operatorEqual(this, that)
    End Operator

    Public Shared Operator <>(ByVal this As signednode(Of T), ByVal that As signednode(Of T)) As Boolean
        Return operatorUnequal(this, that)
    End Operator

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim rtn As signednode(Of T) = Nothing
        rtn = New signednode(Of T)(signingIndex())
        rtn.data = d

        Return rtn
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return sign
    End Function

    Public Overrides Function ToString() As String
        Return Convert.ToString(d)
    End Function

    Public Sub New(ByVal signingIndex As Int32)
        Me._signingIndex = signingIndex
    End Sub
End Class
